package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.common.util.StrUtils;
import com.geyue.core.model.Focus;
import com.geyue.core.model.Student;
import com.geyue.core.model.Teacher;
import com.geyue.core.service.FocusService;
import com.geyue.core.service.LiveService;
import com.geyue.core.service.StudentService;
import com.geyue.core.service.VedioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lyon on 2017/4/20.
 */
@RestController
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    private StudentService studentService;
    @Autowired
    private FocusService focusService;
    @Autowired
    private LiveService liveService;
    @Autowired
    private VedioService vedioService;

    /**
     * 登录
     * @param tel
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Object login(String tel,String password,HttpSession session){

        if(StrUtils.isEmpty(tel) || StrUtils.isEmpty(password))
            return Res.fail("Access interface param is empty!");

        Student student = new Student();
        student.setTel(tel);
        student = studentService.getOne(student);

        if(student == null) {
            return Res.fail("User is not exist!");
        }else if(!password.equals(student.getPassword())) {
            return Res.fail("Username or password incorrect");
        }
        session.setAttribute("student", student);
        return Res.success("Login success!",student);
       /* return "redirect:index";*/
    }

    /**
     * 注册
     * @param student
     * @return
     */
    @RequestMapping(value = "/reg",method = RequestMethod.POST)
    public Object reg(Student student){

        String tel=student.getTel();
        Student student2=studentService.getOneByKey(tel);

        if(student2 != null){

            return Res.fail("用户已经存在!");
        }

        studentService.save(student);

        return Res.success("注册成功!");
    }

    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public Object all(){
        return studentService.all();
    }

    @RequestMapping(value = "/page",method = RequestMethod.GET)
    @ResponseBody
    public Res list(Integer pageSize, Integer pageNum){

        return Res.success("",studentService.getPage(pageNum,pageSize,new Student()));
    }

    /**
     * 根据学生id 查找学生
     * @param stuId
     * @return
     */
    @RequestMapping(value = "/getStudentById",method = RequestMethod.GET)
    @ResponseBody
    public Student getStudentById(Integer stuId){

        return studentService.getOneByKey(stuId);
    }

    /**
     * 更新用户信息
     * @param
     * @return
     */
    @RequestMapping(value = "/updateStudent",method = RequestMethod.POST)
    @ResponseBody
    public int updateStudent(Student student){

        return studentService.update(student);

    }

    /**
     * 根据ID，获取其关注列表
     * @param stuId
     * @return
     */
    @RequestMapping(value = "/getFocusByStuId",method = RequestMethod.GET)
    @ResponseBody
    public Object getFocusByStuId(Integer stuId){

        List<Focus> list=focusService.queryAll(stuId);

        List<Object> focusList=new ArrayList<>();

        for (Focus item: list ) {

            int type=item.getType();

            if(type==1){

                focusList.add(liveService.getOneByKey(item.getFocusId()));
            }else{

                focusList.add(vedioService.getOneByKey(item.getFocusId()));
            }

        }
        return focusList;

    }

    /**
     * 取消关注
     * @param stuId
     * @param focusId
     * @param type
     * @return
     */
    @RequestMapping(value = "/delFocus",method = RequestMethod.GET)
    @ResponseBody
    public Object delFocus(Integer stuId,Integer focusId,Integer type){

        return focusService.deleteFocus(focusId,stuId,type);
    }

    /**
     * 修改密码
     * @param
     * @return
     */
    @RequestMapping(value = "/updatePassword",method = RequestMethod.GET)
    @ResponseBody
    public Res updatePassword(HttpSession session,String oldPassword,String password1,String password2,Integer stuId){

        Student student=studentService.getOneByKey(stuId);

      //Student student=(Student) session.getAttribute("student");

        Student stu=studentService.getOneByKey(student.getId());
        String Password =student.getPassword();
        if(stu.getPassword().equals(oldPassword)){
            if(password1.equals(password2)){
                Student param = new Student();
                param.setId(student.getId());
                param.setPassword(password1);
                if(studentService.updateNotEmpty(param) > 0)
                    return Res.success("修改成功");
            }else{

                return Res.fail("两次输入的密码不一致!");
            }

        }

        return Res.fail("修改失败!");

    }

    /**
     * 忘记密码
     * @param stuId
     * @return
     */
    @RequestMapping(value = "/updatePasswordByStuId",method = RequestMethod.GET)
    @ResponseBody
    public Res updatePasswordByStuId(HttpSession session,Integer stuId,String password1,String password2){

        Student student=studentService.getOneByKey(stuId);

       //Student student=(Student) session.getAttribute("student");
        if(password1.equals(password2)){
            Student param = new Student();
            param.setId(student.getId());
            param.setPassword(password1);
            if(studentService.updateNotEmpty(param) > 0)
                return Res.success("设置成功！");
        }

        return Res.fail("设置失败!");
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping(value = "/loginOut",method = RequestMethod.GET)
    @ResponseBody
    public String loginOut(HttpSession session){

        session.invalidate();

        return "login";
    }




}
